from datetime import date, timedelta

from django.views import View
from rest_framework.response import Response
from rest_framework.permissions import IsAdminUser
from rest_framework.views import APIView

from goods.models import GoodsVisitCount
from meiduo_admin.serializers.statistical import UserGoodsCountSerializer
from users.models import User


class  UserCountView(APIView):
    # 用户总量统计
    permission_classes = [IsAdminUser]
    def get(self,request):
        #获取当天日期datatime
        now_date = date.today()
        #获取用户总量User
        count = User.objects.all().count()
        #返回结果
        return Response({
            'date':now_date,
            'count':count
        })


class UserDayCountView(APIView):
    # 日用户增量统计
    #权限指定
    permission_classes = [IsAdminUser]
    def get(self,request):
        now_date = date.today()
        count = User.objects.filter(date_joined__gte=now_date ).count()
        return Response({
            'count':count,
            'date':now_date

        })

class UserDayActiveCountView(APIView):
    # 日活用户统计
    permission_classes = [IsAdminUser]
    def get(self,request):
        now_date = date.today()
        count = User.objects.filter(last_login__gte=now_date).count()
        return Response({
            'count':count,
            'date':now_date
        })


class UserDayOrderView(APIView):
    # 日下单用户统计
    permission_classes = [IsAdminUser]
    def get(self,request):
        now_date = date.today()
        count = User.objects.filter(orders__create_time__gte=now_date).count()
        return Response({
            'count':count,
            'date':now_date
        })

class UserMouthOrderView(APIView):
    # 月增用户统计
    permission_classes = [IsAdminUser]
    def get(self,request):
        now_date = date.today()
        begin_date = now_date-timedelta(days=29)
        data_list = []
        for i in range(30):
            #起始日期
            index_date = begin_date+timedelta(days=i)
            # 第二天日期
            next_date = begin_date + timedelta(days=i+1)
            count= User.objects.filter(date_joined__gte=index_date,date_joined__lt=next_date).count()
            data_list.append({
                'count':count,
                'date':index_date
            })
        return  Response(data_list)


class UserGoodsCountView(APIView):
    # 日分类商品访问统计

    permission_classes = [IsAdminUser]
    def get(self,request):
        now_date = date.today()
        goods= GoodsVisitCount.objects.filter(date__gte=now_date)
        ser = UserGoodsCountSerializer(goods,many=True)
        return Response(ser.data)






















